package com.example.crawler.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class SendHttpRequestUtil {

    public static String getResultDataString(String url) throws IOException {
        HttpClient httpClient = new HttpClient();
        CloseableHttpResponse response = null;
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("User-Agent", "PostmanRuntime/7.29.2");
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
//        NameValuePair[] data = {
//                new NameValuePair("page", "0"),
//                new NameValuePair("disclosureType", "9530"),
//                new NameValuePair("startTime", "2023-03-16"),
//                new NameValuePair("endTime", "2023-03-17")
//        };
//        postMethod.setRequestBody(data);
        try {
            //使用HttpClient发起请求
            httpClient.executeMethod(postMethod); // 执行POST方法
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放连接
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return postMethod.getResponseBodyAsString();
    }

}
